package com.lanhai.fnstore;

import java.io.File;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
 

public class Tools {
	 private static final Logger logger = Logger.getLogger(Tools.class);
		
	
	public static String byte2HexStr(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs += " 0" + stmp;
			else
				hs += " " + stmp;  
		}
		return hs.toUpperCase().substring(1);
	}
	
	public static short toIntL(byte[] b) {
		short res = 0;
		for (int i = 0; i < b.length; i++) {
			res += (b[i] & 0xff) << (i * 8);
		}
		return res;
	}
	
	
	public static short toIntH(byte[] b){
		short res = 0;
	    for(int i=0;i<b.length;i++){
	        res += (b[i] & 0xff) << ((3-i)*8);
	    }
	    return res;
	}
	
	public static List<Integer> toList(String s) {
		if (s.isBlank())
			return null;
		String m=s.replaceAll("、", ",");
		m=s.replaceAll("，", ","); 
		String[] tmp = m.split(",");
		List<Integer> arr = new ArrayList<Integer>();
		for (String k : tmp) {
			try {
				Integer ty = Integer.parseInt(k);
				arr.add(ty);
			} catch (NumberFormatException ex) {

			}
		} 
		return arr;
	}
	
	public static List<String> toStrList(String s) {
		if (s.isBlank())
			return  new ArrayList<String>();
		String m=s.replaceAll("、", ",");
		m=s.replaceAll("，", ","); 
		String[] tmp = m.split(",");
		List<String> arr = new ArrayList<String>();
		for (String k : tmp) {
			String mp=k.trim();
			if(mp.length()>0) {
				arr.add(mp);
			} 
		} 
		return arr;
	}
	
	/**
	  * 获取某目录下的所有文件
	 * @param directoryPath
	 * @return
	 */
	public static List<String> getAllFile(String directoryPath ) {
        List<String> list = new ArrayList<String>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()==false) {  
                list.add(file.getAbsolutePath());
            }
        }
        return list;
    } 
	
	
	public static void writeErrLog(String msg) {
		
		try {
			HttpClient client = HttpClient.newHttpClient();
			String url = "http://127.0.0.1:5000/api/ErrLog/writeLog";
			String p = "msg=" + URLEncoder.encode(msg, "UTF-8");

			HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url))
					.header("Content-Type", "application/x-www-form-urlencoded")
					.POST(HttpRequest.BodyPublishers.ofString(p)).build();

			HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
			if (response.statusCode() != 200) {
				logger.error("url="+url+" http code="+response.statusCode());
				//System.out.println(response.statusCode() + "");
			} else {
				//System.out.println(response.body());
			}

		} catch (Exception ex) {
			//ex.printStackTrace();
		}
		
	}
	
	
	public static void post(String url, String json) {
		try {
			HttpClient client = HttpClient.newHttpClient();

			HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url))
					.header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(json)).build();

			HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
			if (response.statusCode() != 200) {
				//System.out.println(response.statusCode() + "");
				logger.error("url="+url+" http code="+response.statusCode());
			} else {
				//System.out.println(response.body());
			}

		} catch (Exception ex) {
			//ex.printStackTrace();
		}

	}
}
